import Vue from 'vue'
import VueRouter from 'vue-router'

Vue.use(VueRouter)
import layout from '@/layout/index'
import Login from '@/views/Login'
import Register from "@/views/Register"

import { nanoid } from 'nanoid'

const originalPush = VueRouter.prototype.push;
VueRouter.prototype.push = function push(location) {
    return originalPush.call(this, location).catch((err) => err);
};
// 公共路由
import Home from '@/views/Home'
import Course from '@/views/Course'
import Student from '@/views/Sudent'
import Finance from '@/views/Finance'
import OrderGoods from '@/views/OrderGoods'
import goodsDetails from '@/views/OrderGoods/goodsDetails'
import order from '@/views/OrderGoods/center'

const routes = [
  {
    path: '/',
    name: 'layout',
    redirect: 'home',
   
    component: layout,
    meta: { title: '布局' ,id:nanoid() },
    children:[
      {
        path:'home',
        component:Home,
        meta:{title:'首页',id:nanoid(),icon:"el-icon-house"},
      },
      {
        path:'course',
        component:Course,
        meta:{title:'课程管理',id:nanoid(),icon:"el-icon-menu"}
      },
      {
        path:'student',
        component:Student,
        meta:{title:'学员管理',id:nanoid(),icon:"el-icon-user-solid"}
      }
    ]
   
  },

  {
    path: '/login',
    name:'login',
    meta:{title:'登陆页',id:nanoid()},
    component: Login
  },

  {
    path: '/register',
    name:'register',
    meta:{title:'注册页',id:nanoid()},
    component: Register
  }
]


// 权限路由

const asyncRouters = [
  {
    path:'finance',
    component:Finance,
    meta:{title:'财务管理',id:nanoid(),rules:['管理员'],icon:"el-icon-notebook-2"}
  },
  {
    path:'OrderGoods',
    component:OrderGoods,
    redirect:'OrderGoods/order',
    meta:{title:'订单管理',id:nanoid(),rules:['管理员'],icon:"el-icon-document-copy"},
    children:[
      {
        path:'goodsDetails',
        component:goodsDetails,
        name:'goodsDetails',
        meta:{title:'订单详情',id:nanoid(),rules:['管理员'],icon:"el-icon-document"}
      },
      {
        path:'order',
        component:order,
        name:'order',
        meta:{title:'订单管理',id:nanoid(),rules:['管理员'],icon:"el-icon-set-up"}
      }
    ]
  }
]
//角色
function getAsnycnFun(rule,routes){
  var arr = []
  routes.forEach(item=>{
    if(item.meta.rules.includes(rule)){
      arr.push(item)
    }
  })
  return arr
}



const router = new VueRouter({
     routes
})
import { getUseApi } from '../api/Login'
import { getToken } from '../utils/cookieUtils'
import store from '../store/index.js'
var whileArr = ['/login','/register']
router.beforeEach((to,from,next)=>{
  var path = to.path  //当前路由
  var token = getToken('token')
  if(token){ //已经登录
    if(path == '/login'){ //是不是登陆页面
       next('/') //进入首页
    }else{ 
       if(store.state.routes.length > 0){ 
         next()
       }else{
          getUseApi({token:token}).then(res=>{
            if(res.code == 200){
              store.commit('setUser',res.data)

              var routesData = getAsnycnFun(res.data.rule,asyncRouters)

              console.log('routesData',routesData);
              routes[0].children = [...routes[0].children,...routesData]
              store.commit('setRoutes',routes[0].children)//保存children 作为菜单
              console.log(routes[0].children,"2222")
              router.addRoutes(routes) //动态刷新路由
              next({...to,replace:true}) //保证addRoutes刷新路由完整
            }
          })
       }
      
       
    }
 }else{ //没有的登录
   if(whileArr.includes(path)){ //在白名单 放行
     next()//放行
   }else{ //没在白名单 
     next('/login') 
   }
 }
 next()
})

export default router


